<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/base_user.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>领取优惠券</title>
    <script>
        $(function () {
            $(".content .right .use img").toggle(
                function () {
                    $(this).attr("src","__STATIC__/img_user/down1.png");
                    $(this).parents(".content").next(".tips").css({display:"none"})
                },
                function () {
                    $(this).attr("src","__STATIC__/img_user/top3.png")
                    $(this).parents(".content").next(".tips").css({display:"block"})
                }

            )
        })
    </script>
</head>
{include file="public/href" /}
<body>
<div class="wrap-yhq">
    <div class="header">
        <p>店铺优惠券</p>
        <img src="__STATIC__/img/back.png" class="back" onclick=window.location.href="{:url('mobile/user_category/goods_details')}?goods_id={:input('goods_id')}">
    </div>
    <div class="get">
        <p>点击下方优惠券即可领取</p>
    </div>
    <input type="hidden" id="shop_id" value="{:input('shop_id')}" />
    <div id="list"></div>
</div>
{include file="public/src" /}
<script>
    //layer流加载
    layui.use('flow', function() {
        var $ = layui.jquery;
        var flow = layui.flow;
        flow.load({
            elem: '#list' //流加载容器
            ,isAuto:true
            ,isLazyimg:true
            ,done: function(page, next){ //执行下一页的回调
                var lis = [];
                var shop_id = $('#shop_id').val();
                $.ajax({
                    url:"{:url('mobile/user_category/get_ticket')}",
                    dataType:'json',
                    type:'post',
                    data:{'page':page,'shop_id':shop_id},
                    success:function(t){
                        layui.each(t.data, function(index, item){
                            var is_get = '';
                            if (item.member_coupon.length == 0){
                                is_get = "content";
                            }else{
                                is_get = "main";
                            }
                            lis.push('<div class="center"><div class="'+is_get+'">' +
                                    '<div class="left"><p class="price">&yen;<span> '+item.actual_price+'</span></p></div>' +
                                    '<div class="right"><div onclick="jump_get(this,'+item.id+')"><p class="dress">'+item.title+'</p><p class="num" >剩余<font id="num_'+item.id+'">'+item.number+'</font>张</p></div>' +
                                    '<p class="use"><span>消费满'+item.virtual_price+'元使用 </span>' +
                                    '<img src="__STATIC__/img_user/down1.png" id="img_'+item.id+'" onclick="up_down('+item.id+')"></p></div>' +
                                    '</div><div class="tips" id="tips_'+item.id+'" style="display: none" ><p>'+item.content+'</p></div></div>');
                        });
                        next(lis.join(''), page < t.pages);
                    }
                })
            }
        });
    });
    function up_down(id){
        var $_this = $('#img_'+id);
        var url = $('#img_'+id).prop('src');
        if (url.substr(url.length-9) == "top31.png"){
            $_this.attr("src","__STATIC__/img_user/down1.png")
            $('#tips_'+id).css({display:"none"})
        }else{
            $_this.attr("src","__STATIC__/img_user/top31.png")
            $('#tips_'+id).css({display:"block"})
        }
    }
    //领取优惠券
    function jump_get(o,id){
        //判断是否登录
        if (!'{$Think.session.member_id}'){
            jump_login(); return false;
        }
        var status= $(o).parent('div').parent('div').attr('class');
        if (status == 'main'){layer.open({content:'此优惠券已领取',skin:'msg',time:2});return false;}
        var num = parseInt($('#num_'+id).text());
        if (num <= 0){
            layer.open({content:'数量已空',skin:'msg',time:2});
        }
        $.ajax({
            url:"ticket_act",
            dataType:'json',
            type:'post',
            data:{'id':id},
            success:function (t) {
                if (t.code == 200){
                    layer.open({content:t.message,skin:'msg',time:2});
                    $(o).parent('div').parent('div').attr('class','main');
                    $('#num_'+id).text(num-1);
                }else{
                    layer.open({content:t.message,skin:'msg',time:2});
                }
            },
            error: function () {
                layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
            }
        })
    }
    //未登录跳转到登录
    function jump_login(){
        layer.open({
            content: '您还未登录,请登录'
            ,btn: ['去登录', '取消']
            ,yes: function(index){
                window.location.href="{:url('mobile/user_port/login')}";
                layer.close(index);
            }
        });
    }
</script>
</body>
</html>